<?php
/**
 * 人脉
 */

namespace App\Http\Controllers\App\V3;

use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

class ConnectionController extends BaseController
{

    public function propTargetUsers(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'product_id' => 'required|numeric|gt:0'
            ]
        );
        if ($v->fails()) throw new BasicException(10010, getValidateMessage($v->messages()));

        $productId = $request->input('product_id', 0);
        $result = app('repo_connection')->propTargetUsers($productId);

        return Response::sendData($result);
    }

    public function index(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'user_id' => 'numeric|gt:0'
            ]
        );
        if ($v->fails()) throw new BasicException(10010, getValidateMessage($v->messages()));

        $userId = $request->input('user_id', 0);
        $result = app('repo_connection')->index($userId);

        return Response::sendData($result);
    }

    public function giftReceiveList(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'user_id' => 'numeric|gt:0',
                'page' => 'numeric|gt:0',
                'per_page' => 'numeric|gt:0',
            ]
        );
        if ($v->fails()) throw new BasicException(10010, getValidateMessage($v->messages()));

        $userId = $request->input('user_id', 0);
        $page = $request->input('page', 1);
        $perPage = $request->input('per_page', 20);
        $result = app('repo_connection')->giftReceiveList($userId, $page, $perPage);

        return Response::sendData($result);
    }

    public function intimacyRank(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'user_id' => 'numeric|gt:0',
                'page' => 'numeric|gt:0',
                'per_page' => 'numeric|gt:0',
            ]
        );
        if ($v->fails()) throw new BasicException(10010, getValidateMessage($v->messages()));

        $userId = $request->input('user_id', UID);
        $page = $request->input('page', 1);
        $perPage = $request->input('per_page', 20);
        $result = app('repo_connection')->intimacyRank($userId, $page, $perPage);

        return Response::sendData($result);
    }

    public function cpSearch(Request $request)
    {
        $keyword = trim($request->input('keyword'));
        $result = app('repo_connection')->cpSearch($keyword);

        return Response::sendData($result);
    }

    public function cpApplyBind(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'user_id' => 'required|numeric|gt:0',
            ]
        );
        if ($v->fails()) throw new BasicException(10010);

        $userId = $request->input('user_id');
        $result = app('repo_connection')->cpApplyBind($userId);

        return Response::sendData($result);
    }

    public function cpBind(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric|gt:0',
                'type' => 'required|numeric|in:0,1',
            ]
        );
        if ($v->fails()) throw new BasicException(10010, getValidateMessage($v->messages()));

        $id = $request->input('id');
        $type = $request->input('type');
        $result = app('repo_connection')->cpBind($id, $type);

        return Response::sendData($result);
    }

    public function edit()
    {
        $result = app('repo_connection')->edit();

        return Response::sendData($result);
    }

    public function editUserNickname(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric|gt:0',
                'user_nickname' => 'required|string|max:2'
            ]
        );
        if ($v->fails()) throw new BasicException(10010, getValidateMessage($v->messages()));

        $id = $request->input('id');
        $userNickname = $request->input('user_nickname');
        $result = app('repo_connection')->editUserNickname($id, $userNickname);

        return Response::sendData($result);
    }

    public function unbind(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric|gt:0'
            ]
        );
        if ($v->fails()) throw new BasicException(10010);

        $id = $request->input('id');
        $result = app('repo_connection')->unbind($id);

        return Response::sendData($result);
    }

    public function homepage(Request $request)
    {
        $userId = $request->input('user_id', UID);
        $userId = getRealUserId($userId);
        $result = app('repo_connection')->homepage($userId);

        return Response::sendData($result);
    }

    public function help()
    {
        $detail = app('model_article')->where('article_id', 37)->select('title','content')->first();
        if (!empty($detail)) {
            $result = $detail->toArray();
        } else {
            $result = [
                'title' => '人脉说明',
                'content' => ''
            ];
        }
        return Response::sendData($result);
    }
}
